package com.rrd.itext.xmlBean.layoutBean;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Document")
public class XmlBaseBean {

    @XStreamAlias("Sections")
    private List<XmlSection> sections;

    @XStreamAlias("debugSwitch")
    @XStreamAsAttribute
    private String debugSwitch;
    @XStreamAlias("pageSize")
    @XStreamAsAttribute
    private String pageSize;
    
    @XStreamAlias("Styles")
    private List<XmlStyle> styles;

	public List<XmlSection> getSections() {
		if (sections != null) {
			for (XmlSection section : sections) {
				if (section.getOrderNumber() == null) {
					return sections;
				}
			}
			Comparator comp = new ComparatorImpl();
			Collections.sort(sections, comp);
			return sections;
		}

		return sections;
	}

	public void setSections(List<XmlSection> sections) {
		this.sections = sections;
	}

    public List<XmlStyle> getStyles() {
        return styles;
    }

    public void setStyles(List<XmlStyle> styles) {
        this.styles = styles;
    }

    public int getPageWidth(){
        Map<String,Integer> map = this.getDocumentPageSize();
        return Integer.valueOf(map.get("width"));
    }
    
    public int getPageHeight(){
        Map<String,Integer> map = this.getDocumentPageSize();
        return Integer.valueOf(map.get("height"));
    }
    
    private Map<String,Integer> getDocumentPageSize() {
        Map<String,Integer> pageSizeMap = new HashMap<String,Integer>();
        Map<String,Integer> defaultPageSize = new HashMap<String,Integer>();
        defaultPageSize.put("width", 640);
        defaultPageSize.put("height", 400);
        if(pageSize == null){
            return defaultPageSize;
        }
        try{
            String[] split = this.pageSize.split(",");
            if(split.length != 2){
                return defaultPageSize;
            }
            Integer width = Integer.valueOf(split[0]);
            Integer height = Integer.valueOf(split[1]);
            if(width == null || width <= 0){
                return defaultPageSize;
            }
            if(height == null || height <= 0){
                return defaultPageSize;
            }
            pageSizeMap.put("width", width);
            pageSizeMap.put("height", height);
            return pageSizeMap;
        }catch(Exception e){
            e.printStackTrace();
            return defaultPageSize;
        }
    }
    
    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }

    public String getDebugSwitch() {
        return debugSwitch;
    }

    public void setDebugSwitch(String debugSwitch) {
        this.debugSwitch = debugSwitch;
    }
}
class ComparatorImpl implements Comparator<XmlSection> {
	public int compare(XmlSection s1,XmlSection s2) {
		int age1 = s1.getOrderNumber();
		int age2 = s2.getOrderNumber();
		if(age1>age2){
			return 1;
		}else if(age1<age2){
			return -1;
		}else{
			return 0;
		}
	}
}

